<template>
  <div class="LevelManagement">
    <el-card>
      <h3><span class="tag"></span> 等级经验管理</h3>
      <div class="searchBox">
        <span class="txt">等级：</span>
        <el-input v-model="lv" clearable size="small"></el-input>
        <el-button
          class="btn"
          icon="el-icon-search"
          size="small"
          @click="search"
          >查询搜索</el-button
        >
      </div>
      <div class="typeBox">
        <div></div>
        <div class="right">
          <el-button
            class="btn"
            size="small"
            :loading="btnLoading"
            @click="experienceMannage"
            >经验管理</el-button
          >
          <el-button class="btn" size="small" @click="add">新增等级</el-button>
          <!-- <el-button icon="el-icon-paperclip" size="small">导出EXCEL</el-button> -->
        </div>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%; margin-top: 10px"
        :header-cell-style="{ background: '#f5f7fa' }"
      >
        <template slot="empty">
          <el-empty :image-size="200"></el-empty>
        </template>
        <el-table-column prop="date" label="图标">
          <template slot-scope="scope">
            <el-image
              style="width: 50px; height: 50px"
              :src="scope.row.icon"
              :preview-src-list="[scope.row.icon]"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="minExp" label="经验值"> </el-table-column>
        <el-table-column prop="level" label="等级">
          <template slot-scope="scope">
            {{ scope.row.level + "级" }}
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="small"
              plain
              @click="edit(scope.row)"
              >编辑</el-button
            >
            <el-button type="info" size="small" plain @click="del(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        :total="total"
        :page.sync="page"
        :limit.sync="pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 等级弹窗 -->
    <el-dialog title="等级编辑" :visible.sync="show" width="30%" center>
      <el-form label-position="right" label-width="80px" :model="form">
        <el-form-item label="等级图标">
          <el-upload
            class="avatar-uploader"
            :http-request="upLoad"
            :show-file-list="false"
            action
          >
            <div class="imgbox" v-if="form.icon.path">
              <img :src="form.icon.path" class="avatar" />
              <!-- <i class="el-icon-close" @click.stop="delImg"></i> -->
              <!-- <div class="txt" @click.stop="">{{ form.imageUrl }}</div> -->
            </div>
            <i v-else class="upicon el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="经验值">
          <el-input
            placeholder="请输入所需经验"
            onkeyup="this.value=value.replace(/[^\d{1,}\.\d{1,}|\d{1,}]/g,'')"
            v-model="form.min_exp"
          ></el-input>
        </el-form-item>
        <el-form-item label="等级">
          <el-input
            placeholder="请输入等级"
            onkeyup="this.value=value.replace(/[^\d{1,}\.\d{1,}|\d{1,}]/g,'')"
            v-model="form.level"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="show = false">取 消</el-button>
        <el-button size="small" class="btn" type="primary" @click="submitAdd"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 等级经验管理弹窗 -->
    <el-dialog title="经验管理" :visible.sync="showMaker" width="30%" center>
      <div class="setItem">
        <div class="txt">钻石兑换经验比例</div>
        <el-input v-model.number="formA.goldToExp.gold"> </el-input>
        <div>钻石：</div>
        <el-input v-model.number="formA.goldToExp.exp"></el-input>
        <div>经验</div>
      </div>
      <div class="setItem">
        <div class="txt">叶子兑换经验比例</div>
        <el-input v-model.number="formA.integralToExp.integral"> </el-input>
        <div>叶子：</div>
        <el-input v-model.number="formA.integralToExp.exp"></el-input>
        <div>经验</div>
      </div>
      <div class="setItem">
        <div class="txt">主播时长兑换经验比例</div>
        <el-input v-model.number="formA.anchorTimeToExp.anchorTime"> </el-input>
        <div>min：</div>
        <el-input v-model.number="formA.anchorTimeToExp.exp"></el-input>
        <div>经验</div>
      </div>
      <div class="setItem">
        <div class="txt">用户时长兑换经验比例</div>
        <el-input v-model.number="formA.userTimeToExp.userTime"> </el-input>
        <div>min：</div>
        <el-input v-model.number="formA.userTimeToExp.exp"></el-input>
        <div>经验</div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="showMaker = false">取 消</el-button>
        <el-button size="small" class="btn" type="primary" @click="submitSet"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination"; // 分页
import { client } from "@/utils/oss.js";
import { ossToken } from "@/api/NotificationManagement";
import {
  levelconfigQueryList,
  levelconfigAdd,
  levelconfigDeleteLevel,
  levelconfigGetLevelExpireExchange,
  levelconfigLevelExpireExchange,
} from "@/api/operationManagement";
export default {
  components: { Pagination },
  data() {
    return {
      loading: false,
      btnLoading: false,
      lv: "",
      tableData: [],
      total: 0,
      page: 1,
      pageSize: 10,
      show: false,
      form: {
        icon: {
          path: "",
          width: "",
          height: "",
        },
        min_exp: "",
        level: "",
        id: "",
      },
      showMaker: false,
      formA: {
        goldToExp: {
          exp: "",
          gold: "",
        },
        integralToExp: {
          integral: "",
          exp: "",
        },
        userTimeToExp: {
          exp: "",
          userTime: "",
        },
        anchorTimeToExp: {
          exp: "",
          anchorTime: "",
        },
      },
    };
  },
  methods: {
    async getList() {
      this.loading = true;
      const res = await levelconfigQueryList({
        page: this.page,
        pageSize: this.pageSize,
        level: this.lv,
      });
      this.loading = false;
      if (res.code == 0) {
        this.tableData = res.data.list;
        this.total = res.data.total;
      }
    },
    search() {
      this.page = 1;
      this.getList();
    },
    add() {
      this.show = true;
      this.form = {
        icon: {
          path: "",
          width: "",
          height: "",
        },
        min_exp: "",
        level: "",
        id: "",
      };
    },
    async submitAdd() {
      const res = await levelconfigAdd(this.form);
      if (res.code == 0) {
        this.$message.success("添加成功!");
        this.getList();
        this.show = false;
      }
    },

    edit(obj) {
      this.form.icon = obj.iconInfo;
      this.form.min_exp = obj.minExp;
      this.form.level = obj.level;
      this.form.id = obj.id;
      this.show = true;
    },
    del(id) {
      //levelconfigDeleteLevel
      this.$confirm("此操作将永久删除该等级, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await levelconfigDeleteLevel({
            id,
          });
          if (res.code == 0) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getList();
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //上传
    async upLoad(file) {
      await ossToken({
        type: 0,
      }).then((res) => {
        if (res.code == 0) {
          this.dataObj = {
            accessKeyId: res.data.accessKey,
            accessKeySecret: res.data.accessSecret,
            securityToken: res.data.token,
            region: "oss-" + res.data.regionId,
            bucket: res.data.bucket,
          };
        }
      });
      const self = this;
      let fileNames =
        "/img/" +
        Math.round(new Date() / 1000) +
        "." +
        file.file.name.substring(file.file.name.lastIndexOf(".") + 1);
      console.log(fileNames);
      client(this.dataObj)
        .multipartUpload(fileNames, file.file)
        .then((result) => {
          //下面是如果对返回结果再进行处理，根据项目需要
          this.form.icon.path = "https://img.psychexy.com" + fileNames;
          var images = new Image();
          images.src = this.form.icon.path;
          images.onload = function () {
            console.log("宽度为：", this.width, "高度为：", this.height);
            self.form.icon.width = this.width;
            self.form.icon.height = this.height;
            self.$message({
              message: "上传成功",
              type: "success",
            });
          };
        })
        .catch((err) => {
          self.$message.error("上传失败");
        });
    },
    //等级经验管理
    async experienceMannage() {
      this.btnLoading = true;
      const res = await levelconfigGetLevelExpireExchange();
      this.btnLoading = false;
      if (res.code == 0) {
        this.formA = res.data;
        this.showMaker = true;
      }
    },
    async submitSet() {
      const res = await levelconfigLevelExpireExchange({
        goldToExp: this.formA.goldToExp,
        integralToExp: this.formA.integralToExp,
        userTimeToExp: this.formA.userTimeToExp,
        anchorTimeToExp: this.formA.anchorTimeToExp,
      });
      if (res.code == 0) {
        this.$message.success("修改成功!");
        this.showMaker = false;
      }
    },
  },
  created() {
    this.getList();
  },
};
</script>

<style lang="less" scoped>
.LevelManagement {
  padding: 20px;
  h3 {
    margin: 0 0;
    display: flex;
    align-items: center;
    .tag {
      display: inline-block;
      width: 5px;
      height: 18px;
      background-color: #7b94d8;
      margin-right: 10px;
    }
  }
  .searchBox {
    display: flex;
    align-items: center;
    margin: 20px 0;
    .margin {
      margin: 0 10px;
    }
    .txt {
      display: inline-block;
      height: 32px;
      background-color: #f5f7fa;
      line-height: 32px;
      padding-left: 10px;
      color: #9d9399;
      border-radius: 4px 0 0 4px;
      border: 1px solid #dcdfe6;
      border-right: none;
    }
    /deep/ .el-input__inner {
      border-radius: 0 4px 4px 0 !important;
    }
    .el-input {
      width: 200px;
    }
  }
  .typeBox {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .upicon {
    display: inline-block;
    width: 178px;
    height: 178px;
    line-height: 178px;
    font-size: 24px;
    border: 1px dashed #ccc;
    border-radius: 8px;
  }
  .imgbox {
    position: relative;
    .avatar {
      display: inline-block;
      width: 178px;
      height: 178px;
      line-height: 178px;
      font-size: 24px;
      border: 1px dashed #ccc;
      border-radius: 8px;
      padding: 2px;
    }
    i {
      position: absolute;
      top: -10px;
      right: 40px;
      font-size: 24px;
      font-weight: 700;
      color: #000;
    }
  }
  .setItem {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
    .txt {
      width: 200px;
    }
    div {
      margin: 0 5px;
    }
    .el-input {
      width: 100px;
    }
  }
}
</style>